#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project: DataStructure-Python
@File: Double-Linked-List.py
@Author: Dragon Geek
@Date: 2021/4/19 10:15
@Description: Bubble Sort Algorithm
"""


class Node(object):
    def __init__(self, elem):
        # data element
        self.elem = elem
        # Precursor node
        self.prev = None
        # Successor node
        self.next = None


class DoubleLinkedList(object):
    def __init__(self, node=None):
        self.__head = node

    def is_empty(self):
        return self.__head is None

    def length(self):
        cur = self.__head
        count = 0
        while cur is not None:
            count += 1
            cur = cur.next
        return count

    def travel(self):
        cur = self.__head
        while cur is not None:
            print(cur.elem, end=" ")
            cur = cur.next
        print("")

    def add(self, item):
        node = Node(item)
        if self.is_empty():
            self.__head = node
        else:
            node.next = self.__head
            self.__head = node
            node.next.prev = node

    def append(self, item):
        node = Node(item)
        if self.is_empty():
            self.__head = node
        else:
            cur = self.__head
            while cur.next is not None:
                cur = cur.next
            cur.next = node
            node.prev = cur

    def insert(self, position, item):
        if position <= 0:
            self.add(item)
        elif position >= (self.length() - 1):
            self.append(item)
        else:
            node = Node(item)
            cur = self.__head
            count = 0
            while count != position:
                count += 1
                cur = cur.next
            node.prev = cur.prev
            node.next = cur
            cur.prev.next = node
            cur.prev = node

    def remove(self, item):
        cur = self.__head
        # 判断是否为空列表
        while cur is not None:
            # 判断值是否相等
            if cur.elem == item:
                # 判断此节点是否为头结点
                if cur == self.__head:
                    self.__head = cur.next
                    # 判断链表是否只有一个节点
                    if cur.next:
                        self.__head.prev = None
                else:
                    cur.prev.next = cur.next
                    if cur.next:
                        cur.next.prev = cur.prev
                break
            else:
                cur = cur.next

    def search(self, item):
        cur = self.__head
        while cur.next is not None:
            if cur.elem == item:
                return True
            else:
                cur = cur.next
        return False


if __name__ == "__main__":
    DoubleLinkedList_01 = DoubleLinkedList()
    print(DoubleLinkedList_01.is_empty())
    print(DoubleLinkedList_01.length())
    # DoubleLinkedList_01.add(1)
    # print(DoubleLinkedList_01.length())
    # DoubleLinkedList_01.travel()
    DoubleLinkedList_01.append(1)
    print(DoubleLinkedList_01.is_empty())
    DoubleLinkedList_01.travel()
    DoubleLinkedList_01.append(2)
    DoubleLinkedList_01.append(3)
    DoubleLinkedList_01.append(4)
    DoubleLinkedList_01.append(5)
    DoubleLinkedList_01.append(6)
    DoubleLinkedList_01.travel()
    DoubleLinkedList_01.insert(-1, 9)
    DoubleLinkedList_01.travel()
    DoubleLinkedList_01.insert(3, 100)
    DoubleLinkedList_01.insert(6, 100)
    DoubleLinkedList_01.insert(3, 200)
    DoubleLinkedList_01.travel()
    DoubleLinkedList_01.remove(100)
    DoubleLinkedList_01.travel()
    print(DoubleLinkedList_01.search(100))
    DoubleLinkedList_01.remove(9)
    DoubleLinkedList_01.travel()
    DoubleLinkedList_01.remove(6)
    DoubleLinkedList_01.travel()

